package IO流.会员管理操作系统;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {

    static Map<Integer, String> map = new HashMap<>();
    static Vip vip = new Vip();
    static Scanner input = new Scanner(System.in);
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void open() {
        Date now = new Date();
        String str = sdf.format(now);
        System.out.print("请输入您的注册姓名：");
        vip.setName(input.next());
        System.out.print("请输入您的注册密码：");
        vip.setPassword(input.next());
        vip.setDate(str);
        System.out.print("恭喜！开通会员成功，系统赠送您100积分!");
        vip.setIntegral(100);
        vip.setId((int) (Math.random() * 90000000 + 10000000));
        System.out.println("您的会员卡号为：" + vip.getId());
        map.put(vip.getId(),vip.getPassword());
    }

    public static void register() {
        System.out.print("请输入您的会员卡号：");
        vip.setId(input.nextInt());
        System.out.print("请输入您的会员卡密码：");
        vip.setPassword(input.next());
        if (map.containsKey(vip.getId()) && map.containsValue(vip.getPassword())) {
            System.out.print("请输入您此次的消费金额：(消费亿元累计一积分)");
            int money = input.nextInt();
            vip.setIntegral(vip.getIntegral() + money);
            System.out.println("积分累计成功！");
        } else {
            System.err.println("账号或密码错误");
        }
    }

    public static void conversion() {
        System.out.print("请输入您的会员卡号：");
        vip.setId(input.nextInt());
        System.out.print("请输入您的会员卡密码：");
        vip.setPassword(input.next());
        System.out.print("请输入您需要兑换使用的积分：(100积分抵用0.1元，不足100的积分不能抵用)");
        int integral = input.nextInt();
        if (integral > 0) {
            if (integral > vip.getIntegral()) {
                System.err.println("积分不足！");
            } else {
                vip.setIntegral(vip.getIntegral() - integral);
                System.out.println("您的消费金额中使用会员积分抵消" + integral * 0.001 + "元");
                System.out.println("积分兑换成功！");
            }
        } else {
            System.err.println("请输入正数!");
        }
    }
    public static void inquire(){
        System.out.print("请输入您的会员卡号：");
        vip.setId(input.nextInt());
        System.out.print("请输入您的会员卡密码：");
        vip.setPassword(input.next());
        if (map.containsKey(vip.getId()) && map.containsValue(vip.getPassword())) {
            System.out.println("姓名\t会员卡号\t\t剩余积分\t\t\t开卡日期");
            System.out.println(vip.getName()+"\t"+vip.getId()+"\t"+vip.getIntegral()+"\t\t\t"+vip.getDate());
        }
    }

    public static void amend () {
        System.out.print("请输入您的会员卡号：");
        vip.setId(input.nextInt());
        System.out.print("请输入您的会员卡密码：");
        vip.setPassword(input.next());
        if (map.containsKey(vip.getId()) && map.containsValue(vip.getPassword())) {
            System.out.print("请输入新的会员密码：");
            vip.setPassword(input.next());
            System.out.println("密码修改成功！");
            map.put(vip.getId(),vip.getPassword());
        }
    }

    public static void main(String[] args) {
        do {
            System.out.println();
            System.out.println("===================欢迎进入超市会员管理系统==================");
            System.out.println("1.积分累计  2.积分兑换  3.查询剩余积分  4.修改密码  5.开卡  6.退出");
            System.out.println("========================================================");
            System.out.print("请选择：");
            int num = input.nextInt();
            switch (num) {
                case 1:
                    register();
                    break;
                case 2:
                    conversion();
                    break;
                case 3:
                    inquire();
                    break;
                case 4:
                    amend();
                    break;
                case 5:
                    open();
                    break;
                case 6:
                    return;
                default:
                    System.err.println("敬请期待！");
                    return;
            }
        }while(true);
    }

}

